package com.data.basic.chapter09.leetcode;

import com.data.basic.chapter09.Merger;
import com.data.basic.chapter09.SegmentTree;

/**
 * leetcode 303
 */
class NumArray {

    private SegmentTree<Integer> segmentTree;

    public NumArray(int[] nums) {
        if (nums.length>0){
            Integer[] data = new Integer[nums.length];
            for (int i=0;i<nums.length;i++){
                data[i] = nums[i];
            }
            segmentTree = new SegmentTree<>(data, new Merger<Integer>() {
                @Override
                public Integer merger(Integer a, Integer b) {
                    return a+b;
                }
            });
        }
    }

    public int sumRange(int i, int j) {
        if (segmentTree==null){
            throw new IllegalArgumentException("Segment Tree is null");
        }
      return   segmentTree.query(i,j);

    }
}